﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using LiWi;
namespace LiWi
{
    public  class Data{
        public static IntPtr handle;
        public static AxisDataList axisDataList=new AxisDataList();
        public static bool _pause = false;  //暂停标志

        /// <summary>
        /// 获取剩余缓冲
        /// </summary>
        public static int ReMainBuffer(int[] axisList)
        {
            int haveValue = 0;  //剩余缓冲量
            int minValue = 9999;
            //判断是否还有缓冲
            foreach (var item in axisList)
            {               
                Shell.PMC_GetRemain_Buffer(Data.handle, item, ref haveValue);                
                if (haveValue < minValue) minValue= haveValue;
            }
            return minValue;
        }


        public static void XmlSerializeFile<T>(T obj,string filePath) {
            string xml = "";
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    serializer.Serialize(writer, obj);
                    xml = sww.ToString();
                }
            }
            FileStream fileStream=new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
            byte[] buffer = Encoding.Unicode.GetBytes(xml);
            fileStream.Write(buffer, 0, buffer.Length);
            fileStream.Close();
        }

        public static bool DeserializeXmlFile<T>(string filePath,ref T src) {
            try
            {
                FileStream myFileStream = new FileStream(filePath, FileMode.Open);
                var mySerializer = new XmlSerializer(typeof(T));
                T myObject = (T)mySerializer.Deserialize(myFileStream);
                myFileStream.Close();
                src= myObject;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool GetBitValue(int value, ushort index)
        {
             if (index > 31) throw new ArgumentOutOfRangeException("index"); //索引出错
            var val = 1 << index;
            return (value & val) == val;
        }


}

    public class AxisData
    {
        public AxisData(int axisNumber, int axisType=1, int axisInvertStep=0, float units=1, float speed=100, float creep=0, float accel=1000, float decel=1000, float lspeed=0, float sramp=0, float fsLimit=200000, float rsLimit=-200000, int fwdIn=-1, int revIn=-1, int datumIn=-1, bool fwdInInvert = false, bool revInInvert=false, bool datumInInvert=false)
        {
            this.axisNumber = axisNumber;
            this.axisType = axisType;
            this.axisInvertStep = axisInvertStep;
            this.units = units;
            this.speed = speed;
            this.accel = accel;
            this.decel = decel;
            this.lspeed = lspeed;
            this.sramp = sramp;
            this.fsLimit = fsLimit;
            this.rsLimit = rsLimit;
            this.fwdIn = fwdIn;
            this.revIn = revIn;
            this.datumIn = datumIn;
            this.fwdInInvert = fwdInInvert;
            this.revInInvert = revInInvert;
            this.datumInInvert = datumInInvert;
            this.creep = creep;
        }
        public AxisData() { 
        
        }

        public int axisNumber { get; set; }
        public int axisType { get; set; }
        public int axisInvertStep { get; set; }
        public float units { get; set; }
        public float speed { get; set; }
        public float accel { get; set; }
        public float decel { get; set; }
        public float lspeed { get; set; }
        public float creep { get; set; }
        public float sramp { get; set; }
        public float fsLimit { get; set; }
        public float rsLimit { get; set; }
        public int fwdIn { get; set; }
        public int revIn { get; set; }
        public int datumIn { get; set; }
        public bool fwdInInvert { get; set; }
        public bool revInInvert { get; set; }
        public bool datumInInvert { get; set; }
    }

    public class AxisDataList:List<AxisData>
    { 
        public AxisDataList() {
        }

        public AxisDataList(List<AxisData> axisDatas)
        {
            Clear();
            AddRange(axisDatas);

        }

    }
}
